package com.tfos.study.upload;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * 类功能说明：SpringBoot2.x之MultipartFile实现文件上传测试控制器 <br/>
 * 公司名称：TF（腾飞）开源 <br/>
 * 作者：luorongxi <br/>
 */
@Controller
public class TestUploadController {

    @Value("${uploadFolder}")
    private String  fileDir;

    /**
     * 跳转到单文件上传页面
     * @return
     */
    @RequestMapping("/singleFileUploadUi")
    public String singleFileUploadUi(){
        //跳转到singleFileUpload.html
        return "singleFileUpload";
    }

    /**
     * 单文件上传方法
     * @param map
     * @param fileUpload
     * @return
     */
    @PostMapping("/singleFileUpload")
    public String upload(ModelMap map, MultipartFile fileUpload){
        //获取文件名
        String fileName = fileUpload.getOriginalFilename();
        //获取文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //重新生成文件名
        fileName = UUID.randomUUID()+suffixName;
        //指定本地文件夹存储图片
        try {
            System.out.println("------->>"+fileDir);
            File dir = new File(fileDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File upload_file = new File(fileDir + fileName);
            fileUpload.transferTo(upload_file);
            map.addAttribute("msg", fileDir + fileName);
        } catch (Exception e) {
            e.printStackTrace();
            map.addAttribute("msg", "fail to upload");
        }
        return "singleFileUpload";
    }

    /**
     * 跳转到批量文件上传页面
     * @return
     */
    @RequestMapping("/multipartFileUploadUi")
    public String multiUpload(){
        //跳转到multipartFileUpload.html
        return "multipartFileUpload";
    }

    /**
     * 批量文件上传方法
     * @param map
     * @param fileUpload
     * @return
     */
    @RequestMapping(value = "/uploadMultifile", method = RequestMethod.POST)
    public String uploadMultiFile(ModelMap map, @RequestParam("attachment") MultipartFile[] fileUpload) {
        try {
            String urls = "";
            for (int i=0;i<fileUpload.length;i++){
                //获取文件名
                String fileName = fileUpload[i].getOriginalFilename();
                //指定本地文件夹存储图片
                File dir = new File(fileDir);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                if (fileUpload[i]!=null){
                    File upload_file = new File(fileDir + fileName);
                    fileUpload[i].transferTo(upload_file);
                }
                urls = urls+","+fileDir + fileName;
            }
            if(!ObjectUtils.isEmpty(urls)){
                urls = urls.substring(urls.indexOf(",")+1);
            }
            map.addAttribute("msg", urls);
        } catch (Exception e) {
            e.printStackTrace();
            map.addAttribute("msg", "fail to upload");
        }
        return "multipartFileUpload";
    }
}
